package com.hmdp.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Blog;
import com.hmdp.service.IBlogService;
import com.hmdp.utils.SystemConstants;
import com.hmdp.utils.UserHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 豪哥
 */
@Api(tags = "博客相关接口")
@RestController
@RequestMapping("/blog")
public class BlogController {

    @Resource
    private IBlogService blogService;

    @ApiOperation(value = "发布博客", notes = "发布一篇新的博客")
    @ApiImplicitParam(name = "blog", value = "博客信息", required = true, dataType = "Blog")
    @PostMapping
    public Result saveBlog(@RequestBody Blog blog) {
        return blogService.saveBlog(blog);
    }

    @ApiOperation(value = "点赞博客", notes = "给指定博客点赞")
    @ApiImplicitParam(name = "id", value = "博客id", required = true, dataType = "Long", paramType = "path")
    @PutMapping("/like/{id}")
    public Result likeBlog(@PathVariable("id") Long id) {
        return blogService.likeBlog(id);
    }

    @ApiOperation(value = "查询我的博客", notes = "分页查询当前用户的博客列表")
    @ApiImplicitParam(name = "current", value = "当前页码", defaultValue = "1", dataType = "Integer")
    @GetMapping("/of/me")
    public Result queryMyBlog(@RequestParam(value = "current", defaultValue = "1") Integer current) {
        // 获取登录用户
        UserDTO user = UserHolder.getUser();
        // 根据用户查询
        Page<Blog> page = blogService.query()
                .eq("user_id", user.getId()).page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
        // 获取当前页数据
        List<Blog> records = page.getRecords();
        return Result.ok(records);
    }

    @ApiOperation(value = "查询热门博客", notes = "分页查询热门博客信息")
    @ApiImplicitParam(name = "current", value = "当前页码", defaultValue = "1", dataType = "Integer")
    @GetMapping("/hot")
    public Result queryHotBlog(@RequestParam(value = "current", defaultValue = "1") Integer current) {
        return blogService.queryHotBlog(current);
    }

    @ApiOperation(value = "查询博客详情", notes = "根据博客id查询博客详细信息")
    @ApiImplicitParam(name = "id", value = "博客id", required = true, dataType = "Long", paramType = "path")
    @GetMapping("/{id}")
    public Result queryBlogById(@PathVariable("id") Long id) {
        return blogService.queryBlogById(id);
    }

    @ApiOperation(value = "查询博客点赞列表", notes = "查询给这篇博客点赞的人")
    @ApiImplicitParam(name = "id", value = "博客id", required = true, dataType = "Long", paramType = "path")
    @GetMapping("/likes/{id}")
    public Result queryBlogLikes(@PathVariable("id") Long id) {
        return blogService.queryBlogLikes(id);
    }

    @ApiOperation(value = "查询用户的博客", notes = "分页查询指定用户的博客列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "current", value = "当前页码", defaultValue = "1", dataType = "Integer"),
        @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "Long")
    })
    @GetMapping("/of/user")
    public Result queryBlogByUserId(
            @RequestParam(value = "current", defaultValue = "1") Integer current,
            @RequestParam("id") Long id) {
        // 根据用户查询
        Page<Blog> page = blogService.query()
                .eq("user_id", id).page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
        // 获取当前页数据
        List<Blog> records = page.getRecords();
        return Result.ok(records);
    }

    @ApiOperation(value = "查询关注的博客", notes = "分页查询关注的人发布的博客列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "lastId", value = "上一次查询的最后一条博客id", required = true, dataType = "Long"),
        @ApiImplicitParam(name = "offset", value = "偏移量", defaultValue = "0", dataType = "Integer")
    })
    @GetMapping("/of/follow")
    public Result queryBlogOfFollow(
            @RequestParam("lastId") Long max, @RequestParam(value = "offset", defaultValue = "0") Integer offset){
        return blogService.queryBlogOfFollow(max, offset);
    }
}
